package com.example.smart_city_0829.activity.lawyer_consultation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.lawyer_onsultation.Consultation;
import com.example.smart_city_0829.entity.lawyer_onsultation.Lawyer;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.google.gson.reflect.TypeToken;

import java.util.Calendar;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class EvaluationActivity extends BaseActivity {

    private CardView c1;
    private ImageView im;
    private TextView name;
    private TextView goodAtTv;
    private TextView serverTimeTv;
    private TextView favorableRateTv;
    private CardView cardView4;
    private RatingBar ratingBar;

    private EditText et;
    private Button evaluationBt;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_evaluation;
    }

    @Override
    protected void initView() {
        c1 = (CardView) findViewById(R.id.c1);
        im = (ImageView) findViewById(R.id.im);
        name = (TextView) findViewById(R.id.name);
        goodAtTv = (TextView) findViewById(R.id.goodAt_tv);
        serverTimeTv = (TextView) findViewById(R.id.server_time_tv);
        favorableRateTv = (TextView) findViewById(R.id.favorableRate_tv);
        cardView4 = (CardView) findViewById(R.id.cardView4);
        ratingBar = (RatingBar) findViewById(R.id.rating_bar);
        et = (EditText) findViewById(R.id.et);
        evaluationBt = (Button) findViewById(R.id.evaluation_bt);


    }

    private Consultation.RowsBean data;

    @Override
    protected void initData() {
        data = gson.fromJson(getIntent().getStringExtra("data"), new TypeToken<Consultation.RowsBean>() {
        }.getType());

        if (data != null) {
            //TODO 加载律师详细信息
            api.getLawyer(data.getLawyerId()).enqueue(new NetCallBack<Lawyer>() {
                @Override
                public void onResponse(Call<Lawyer> call, Response<Lawyer> response) {
                    if (response.body() == null) return;
                    final Lawyer.DataBean lawyer = response.body().getData();
                    Glide.with(context)
                            .load(Constant.BASE_URL + lawyer.getAvatarUrl())
                            .into(im);

                    name.setText(String.valueOf(lawyer.getName()));
                    goodAtTv.setText("法律专长：" + lawyer.getLegalExpertiseName());
                    serverTimeTv.setText("咨询人数：" + lawyer.getServiceTimes());
                    favorableRateTv.setText("好评率：" + lawyer.getFavorableRate() + "%");
                }
            });
        }
    }

    @Override
    protected void initEvent() {
        evaluationBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final HashMap<String, Object> params = new HashMap<>();
                params.put("evaluate", et.getText().toString());
                params.put("id", data.getId());
                params.put("score",(int)ratingBar.getRating());
                api.postLawyerEvaluate(RequestBody.create(gson.toJson(params), MediaType.parse(Constant.MediaTypeParseJsonValue))).enqueue(new NetCallBack<Base>() {
                    @Override
                    public void onResponse(Call<Base> call, Response<Base> response) {
                        if (response.body() == null) {
                            showToastShort("评价失败");
                        }

                        showToastShort(String.valueOf(response.body().getMsg()));
                        showToastShort("评价成功");
                        EvaluationActivity.this.finish();
                    }
                });
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "咨询评价";
    }
}